#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <err.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/msnetwork.h>
#include <libmscommon/msepoll.h>
#include <libmscommon/mstime.h>
#include <libmscommon/msmd.h>
#include <libmscommon/msenv.h>
#include <libmslog/mslog.h>
#include <libmsprotocol/msprotocol.h>
#define FLAG "ccx"

int main(int argc, char **argv)
{
	mslog_api_setopt(53,ms_null,0);
	if(argc<4){
		ms_error("Userage:%s  total_connect  pertime  ip:port  [logopt]" ,argv[0]);
		return 0;
	}
	msprotocol_api_init();
	register_protocol();
	msmd_api_setulimit();
	
	ms_debug("Get param");
	ms_byte hostname[1024]={0};
	ms_u32 total_connect=ms_atoi(argv[1]);
	ms_u32 pertime=ms_atoi(argv[2]);
	ms_strcpy(hostname, (argv[3]));
	ms_s32 logopt=(4==argc) ? ms_atoi(argv[4]) : 51;
	mslog_api_setopt(logopt,ms_null,0);
	ms_debug("total_connect:%d,pertime:%d", total_connect,pertime);

	URLProtocol *mshttp_ptl=msprotocol_api_match("tcp");
	URLContext *pcilent_ctt_list=ms_null;
	ms_malloc_ret(pcilent_ctt_list,(total_connect*sizeof(URLContext)),"pcilent_ctt_list",-1);
	ms_memset(pcilent_ctt_list,0,total_connect*sizeof(URLContext));
	ms_u32 index=0;
	URLContext *pcilent_ctt=ms_null;
	for(index=0;index<total_connect;index++){ 
		pcilent_ctt=&pcilent_ctt_list[index];
		ms_sprintfs(pcilent_ctt->url,"tcp://%s",hostname);
		mshttp_ptl->url_open(pcilent_ctt);
	}
	
	while(1){
		
	}
	
	for(index=0;index<total_connect;index++){ 
		pcilent_ctt=&pcilent_ctt_list[index];
		mshttp_ptl->url_close(pcilent_ctt);
	}
end:	
	ms_demalloc(pcilent_ctt_list);
	msprotocol_api_deinit();
	mslog_api_unsetopt();
}

#undef MAIN_C

